package it.unibo.android.convoycruisecontrol.chiefboatsubsystem;

import java.io.File;

import android.app.Activity;
import android.view.ViewGroup;

import dalvik.system.DexClassLoader;
import it.unibo.android.convoycruisecontrol.boatsubsystem.DashboardView;
import it.unibo.android.convoycruisecontrol.boatsubsystem.MyMapView;
import it.unibo.android.location.AndroidLocationDeviceFactory;
import it.unibo.contact.chiefBoatSubSystem.ChiefBoatSubSystemLauncher;
import it.unibo.convoycruisecontrol.boatsubsystem.IBoatFactory;
import it.unibo.convoycruisecontrol.boatsubsystem.ObservableDashboard;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.IControlPanel;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.IFlagDisplayFactory;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.ObservableFlagDisplay;
import it.unibo.convoycruisecontrol.progettodesignandroid.R;
import it.unibo.convoycruisecontrol.progettodesignandroid.R.id;
import it.unibo.convoycruisecontrol.progettodesignandroid.R.layout;
import it.unibo.location.LocationDevice;


public class AndroidChiefBoatSubSystemLauncher extends ChiefBoatSubSystemLauncher {

	Activity activity;
	
	public AndroidChiefBoatSubSystemLauncher(Activity activity)
	{
		this.activity = activity;
	}
	
	@Override
	protected IFlagDisplayFactory getFlagDisplayFactory(String className) 
	{
		String[] data = className.split(":");
		
		final String path = "/data/convoy/" + data[0];
	    
	    File dexOutputDir = activity.getApplicationContext().getDir("dex", 0);
	    DexClassLoader myClassLoader2 = new DexClassLoader(path, dexOutputDir.getAbsolutePath(), null, this.getClass().getClassLoader());
	    
	    IFlagDisplayFactory displayFactory = null;
		try 
		{
			Class<?> clazz = myClassLoader2.loadClass(data[1]);
			displayFactory = (IFlagDisplayFactory)clazz.newInstance();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	    
		return displayFactory;
	}
	
	@Override
	protected void initGui() 
	{
		activity.setContentView(it.unibo.convoycruisecontrol.progettodesignandroid.R.layout.main);		

		DashboardView dashView = new DashboardView(activity, (ObservableDashboard) dashboard);
		MyMapView viewMap = new MyMapView(activity, (ObservableDashboard) dashboard);

		ViewGroup container = (ViewGroup) activity.findViewById(it.unibo.convoycruisecontrol.progettodesignandroid.R.id.myLayout);

		FlagDisplayView flagDisplayView = new FlagDisplayView(activity, (ObservableFlagDisplay)flagDisplay);
		ControlPanelController controlPanelController = new ControlPanelController(activity, (IControlPanel)controlPanel);
		
		container.addView(controlPanelController);
		container.addView(flagDisplayView);
		
		container.addView(dashView);
		container.addView(viewMap);
	}
	
	@Override
	protected IBoatFactory getBoatFactory(String className) 
	{
		String[] data = className.split(":");
		
		final String path = "/data/convoy/" + data[0];
		
	    File dexOutputDir = activity.getApplicationContext().getDir("dex", 0);
	    DexClassLoader myClassLoader2 = new DexClassLoader(path, dexOutputDir.getAbsolutePath(), null, activity.getClassLoader());
	    
	    IBoatFactory boatFactory = null;
		try 
		{
			Class<?> clazz = myClassLoader2.loadClass(data[1]);
			boatFactory = (IBoatFactory)clazz.newInstance();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	    
		return boatFactory;
	}

	@Override
	protected LocationDevice getLocationDevice(String deviceName) 
	{
		AndroidLocationDeviceFactory factory = new AndroidLocationDeviceFactory();
		return factory.createDeviceByName(deviceName);
	}

}
